package com.example.plant.db;

import java.util.ArrayList;

import com.example.plant.Tree;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
//import java.util.HashMap;
//import java.util.Hashtable;

public class DBHelper extends SQLiteOpenHelper {
	public static final String DATABASE_NAME = "MyDB.db";
	public static final String DATA_TABLE_NAME = "data";
	public static final int DATABASE_VERSION = 2;
	
	public static final String DATA_COLUMN_ID = "id";
	public static final String DATA_COLUMN_LO = "lo";
	public static final String DATA_COLUMN_CAY = "cay";
	public static final String DATA_COLUMN_NGAY = "ngay";
	public static final String DATA_COLUMN_TTRANG = "ttrang";
	public static final String DATA_COLUMN_KTHUOC = "kthuoc";
	public static final String DATA_COLUMN_CDINH = "cdinh";
	public static final String DATA_COLUMN_BCHE = "bche";
	public static final String DATA_COLUMN_CHCAO = "chcao";
	public static final String DATA_COLUMN_CHVI = "chvi";
	public static final String DATA_COLUMN_LA = "la";
	public static final String DATA_COLUMN_TCANH = "tcanh";
	public static final String DATA_COLUMN_LCO = "lco";
	public static final String DATA_COLUMN_SBENH = "sbenh";
	public static final String DATA_COLUMN_DOAM = "doam";
	public static final String DATA_COLUMN_TIMEBON = "timebon";
	public static final String DATA_COLUMN_LUONGBON = "luongbon";
	public static final String DATA_COLUMN_TIMEPHUN = "timephun";
	public static final String DATA_COLUMN_LUONGPHUN = "luongphun";
	public static final String DATA_COLUMN_SLCAY = "slcay";
	public static final String DATA_COLUMN_SLLUONG = "slluong";
	public static final String DATA_COLUMN_DGIA = "dgia";
	public static final String DATA_COLUMN_VDKHAC = "vdkhac";
	
		
	
	// public HashMap hp;

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-gennerated method stud
		db.execSQL("create table data"
				+ "(id integer primary key, lo text, cay text, ngay text, ttrang text, kthuoc text, "
				+ "cdinh text, bche text, chcao text, chvi text, la text, tcanh text, lco text, sbenh text, " 
				+ "doam text, timebon text, luongbon text, timephun text, luongphun text, slcay text, " 
				+ "slluong text, dgia text, vdkhac text)");
		
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS data");
		this.onCreate(db);
	}

	public boolean insertTree(Tree tree) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();

		contentValues.put("lo", tree.getLo());
		contentValues.put("cay", tree.getCay());
		contentValues.put("ngay", tree.getNgay());
		contentValues.put("ttrang", tree.getTtrang());
		contentValues.put("kthuoc", tree.getKthuoc());
		contentValues.put("cdinh", tree.getBche());
		contentValues.put("chcao", tree.getChCao());
		contentValues.put("chvi", tree.getCvi());
		contentValues.put("la", tree.getLa());
		contentValues.put("tcanh", tree.getTcanh());
		contentValues.put("lco", tree.getLco());
		contentValues.put("sbenh", tree.getSbenh());
		contentValues.put("doam", tree.getDoam());
		contentValues.put("timebon", tree.getTimebon());		
		contentValues.put("luongbon", tree.getLuongbon());
		contentValues.put("timephun", tree.getTimephun());
		contentValues.put("luongphun", tree.getLuongphun());
		contentValues.put("slcay", tree.getSlcay());
		contentValues.put("slluong", tree.getSlluong());
		contentValues.put("dgia", tree.getDgia());
		contentValues.put("vdkhac", tree.getVdkhac());		

		db.insert("data", null, contentValues);
		return true;
	}

	public Tree getTree(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Tree tree = new Tree();
		Cursor res = db
				.rawQuery("select * from data where id like '" + id + "'", null);
		if (res != null) {
			res.moveToFirst();
		}
		tree.setLo(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_LO)));
		tree.setCay(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_CAY)));
		tree.setNgay(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_NGAY)));
		tree.setTtrang(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_TTRANG)));
		tree.setKthuoc(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_KTHUOC)));
		tree.setCdinh(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_CDINH)));
		tree.setCvi(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_CHVI)));
		tree.setChCao(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_CHCAO)));
		tree.setLa(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_LA)));
		tree.setTcanh(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_TCANH)));
		tree.setLco(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_LCO)));
		tree.setSbenh(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_SBENH)));
		tree.setDoam(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_DOAM)));
		tree.setTimebon(res.getInt(res.getColumnIndex(DBHelper.DATA_COLUMN_TIMEBON)));
		tree.setLuongbon(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_LUONGBON)));
		tree.setTimephun(res.getInt(res.getColumnIndex(DBHelper.DATA_COLUMN_TIMEPHUN)));
		tree.setLuongphun(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_LUONGPHUN)));
		tree.setSlcay(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_SLCAY)));
		tree.setSlluong(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_SLLUONG)));
		tree.setDgia(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_DGIA)));
		tree.setVdkhac(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_VDKHAC)));
		
		return tree;
	}

	public int numberOfRows() {
		SQLiteDatabase db = this.getReadableDatabase();
		int numRows = (int) DatabaseUtils.queryNumEntries(db, DATA_TABLE_NAME);
		return numRows;
	}

	public boolean updateData(Integer id, Tree tree) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();

		contentValues.put("lo", tree.getLo());
		contentValues.put("cay", tree.getCay());
		contentValues.put("ngay", tree.getNgay());
		contentValues.put("ttrang", tree.getTtrang());
		contentValues.put("kthuoc", tree.getKthuoc());
		contentValues.put("cdinh", tree.getBche());
		contentValues.put("chcao", tree.getChCao());
		contentValues.put("chvi", tree.getCvi());
		contentValues.put("la", tree.getLa());
		contentValues.put("tcanh", tree.getTcanh());
		contentValues.put("lco", tree.getLco());
		contentValues.put("sbenh", tree.getSbenh());
		contentValues.put("doam", tree.getDoam());
		contentValues.put("timebon", tree.getTimebon());		
		contentValues.put("luongbon", tree.getLuongbon());
		contentValues.put("timephun", tree.getTimephun());
		contentValues.put("luongphun", tree.getLuongphun());
		contentValues.put("slcay", tree.getSlcay());
		contentValues.put("slluong", tree.getSlluong());
		contentValues.put("dgia", tree.getDgia());
		contentValues.put("vdkhac", tree.getVdkhac());	

		db.update("data", contentValues, "id= ? ",
				new String[] { Integer.toString(id) });
		return true;
	}

	public Integer deleteData(Integer id) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.delete("data", "id = ?",
				new String[] { Integer.toString(id) });
	}

	public ArrayList<Tree> getAllTree() {
		ArrayList<Tree> listTree = new ArrayList<Tree>();
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor res = db.rawQuery("select* from data", null);
		if (res.moveToFirst()) {
			do {
				Tree tree = new Tree();
				tree.setLo(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_LO)));
				tree.setCay(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_CAY)));
				tree.setNgay(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_NGAY)));
				tree.setTtrang(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_TTRANG)));
				tree.setKthuoc(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_KTHUOC)));
				tree.setCdinh(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_CDINH)));
				tree.setCvi(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_CHVI)));
				tree.setChCao(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_CHCAO)));
				tree.setLa(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_LA)));
				tree.setTcanh(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_TCANH)));
				tree.setLco(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_LCO)));
				tree.setSbenh(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_SBENH)));
				tree.setDoam(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_DOAM)));
				tree.setTimebon(res.getInt(res.getColumnIndex(DBHelper.DATA_COLUMN_TIMEBON)));
				tree.setLuongbon(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_LUONGBON)));
				tree.setTimephun(res.getInt(res.getColumnIndex(DBHelper.DATA_COLUMN_TIMEPHUN)));
				tree.setLuongphun(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_LUONGPHUN)));
				tree.setSlcay(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_SLCAY)));
				tree.setSlluong(res.getDouble(res.getColumnIndex(DBHelper.DATA_COLUMN_SLLUONG)));
				tree.setDgia(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_DGIA)));
				tree.setVdkhac(res.getString(res.getColumnIndex(DBHelper.DATA_COLUMN_VDKHAC)));
				
				listTree.add(tree);
			} while (res.moveToNext());
		}
		return listTree;
	}
}
